package io.renren.modules.security.user;

import io.renren.common.constant.Constant;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;

/**
 * 用户
 *
 * @author Mark sunlightcs@gmail.com
 */
public class SecurityUser {

    public static Subject getSubject() {
        try {
            return SecurityUtils.getSubject();
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 获取用户信息
     */
    public static UserDetail getUser() {
        Subject subject = getSubject();
        if (subject == null) {
            return createDefaultUserDetail();
        }

        UserDetail user = (UserDetail) subject.getPrincipal();
        if (user == null) {
            return createDefaultUserDetail();
        }

        return user;
    }

    private static UserDetail createDefaultUserDetail() {
        UserDetail userDetail = new UserDetail();
        userDetail.setId(Constant.SUPER_ADMIN);
        userDetail.setRealName(Constant.SUPER_ADMIN_NAME);
        return userDetail;
    }

    /**
     * 获取用户ID
     */
    public static Long getUserId() {
        return getUser().getId();
    }

    /**
     * 获取部门ID
     */
    public static Long getDeptId() {
        return getUser().getDeptId();
    }
}